package com.acme.domain.client;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.acme.domain.account.Account;

public class Client implements Serializable
{
	private static final long serialVersionUID = 1L;
	
	private String name;
	private Gender gender;
	private List<Account> accounts = new ArrayList<Account>();

	public Client(final String clientName, final Gender sex)
	{
		this.name = clientName;
		this.gender = sex;
	}

	public String getName()
	{
		return name;
	}

	public String getSalutation()
	{
		if (gender != null)
		{
			return gender.getSalutation() + " " + name;
		}
		else
		{
			return name;
		}
	}
	
	public Account getAccount(final int id)
	{
		for (Account acc : accounts)
		{
			if (acc.getID() == id)
			{
				return acc;
			}
		}
		return null;
	}

	public void addAccount(final Account acc)
	{
		accounts.add(acc);
	}
	
	public List<Account> getAccountsList()
	{
		return Collections.unmodifiableList(accounts);
	}

	@Override
	public String toString()
	{
		StringBuffer accs = new StringBuffer(getSalutation() + " accounts:\n");
		for (Account acc : accounts)
		{
			accs.append(acc);
			accs.append("\n");
		}
		return accs.toString();
	}
}
